package model.displayer;

import controller.MainController;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Polygon;
import model.Point;

class TriangleDisplayer extends ShapeDisplayerJFxImpl
{
    public static final Double[] DEFAULT_POINTS = new Double[]{20.0,10.0,0.0,30.0,40.0,30.0};
    public static final int TRIANGLE_POINTS_NUM = 3;

    private Polygon triangle;

    TriangleDisplayer(Pane canvas, MainController controller)
    {
        super(canvas,controller);
        this.shape = new Polygon();
        shape.setStroke(DEFAULT_STROKE);
        shape.setFill(DEFAULT_FILL);
        shape.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        shape.setOnMouseClicked((MouseEvent e)->controller.handleShapeClicked(this));

        this.triangle = (Polygon)this.shape;
        triangle.getPoints().addAll(DEFAULT_POINTS);
    }

    @Override
    public void setLocation(Point start, Point end)
    {
        Point peak = new Point(start.getX() + ( end.getX() - start.getX()) / 2 , start.getY());
        Point p1 = new Point(start.getX() , end.getY());
        Point p2 = new Point(end.getX() , end.getY());

        triangle.getPoints().clear();
        triangle.getPoints().addAll(peak.getX(),peak.getY(),p1.getX(),p1.getY(),p2.getX(),p2.getY());
    }

    @Override
    public ShapeDisplayer clone()
    {
        TriangleDisplayer copy = new TriangleDisplayer(this.canvas,this.controller);
        //删除默认位置的点
        copy.triangle.getPoints().clear();

        Point[] points = this.getTriangleVertex();
        for(Point p : points)
            copy.triangle.getPoints().addAll(p.getX(),p.getY());

        copy.triangle.setStroke(this.triangle.getStroke());
        copy.triangle.setStrokeWidth(DEFAULT_STROKE_WIDTH);
        return copy;
    }

    private Point[] getTriangleVertex()
    {
        Point[] points = new Point[TRIANGLE_POINTS_NUM];
        int index=0;
        System.out.println(triangle.getPoints());
        for(Double d:triangle.getPoints())
        {
            int pIndex = index / 2;
            if(index % 2 == 0)
            {
                points[pIndex]=new Point();
                points[pIndex].setX(d);
            }
            else points[pIndex].setY(d);

            index++;
        }
        return points;
    }

    @Override
    public void resize(double multi)
    {
        Point[] points = getTriangleVertex();
        Point peak = points[0];
        Point p1 = points[1];
        Point p2 = points[2];

        double height = p1.getY()- peak.getY();
        double width = Math.abs( p1.getX() - p2.getX() );
        height *= (1 + multi);
        width *= (1 + multi);

        //peak point unchang
        p1.setY(peak.getY() + height);
        p1.setX(peak.getX() - width / 2);

        p2.setY(peak.getY() + height);
        p2.setX(peak.getX() + width / 2);

        triangle.getPoints().clear();
        triangle.getPoints().addAll(peak.getX(),peak.getY(),p1.getX(),p1.getY(),p2.getX(),p2.getY());
    }

    @Override
    protected Point getCenter()
    {
        Point[] points = getTriangleVertex();
        Point peak = points[0];
        return peak;
    }

    @Override
    protected void setCenter(Point newCenter)
    {
        Point[] points = getTriangleVertex();
        Point peak = points[0];
        Point p1 = points[1];
        Point p2 = points[2];

        double height = p1.getY()- peak.getY();
        double width = Math.abs( p1.getX() - p2.getX() );

        peak.setX(newCenter.getX());
        peak.setY(newCenter.getY());

        p1.setY(peak.getY() + height);
        p1.setX(peak.getX() - width / 2);

        p2.setY(peak.getY() + height);
        p2.setX(peak.getX() + width / 2);

        triangle.getPoints().clear();
        triangle.getPoints().addAll(peak.getX(),peak.getY(),p1.getX(),p1.getY(),p2.getX(),p2.getY());
    }
}
